//
//  AppDelegate.swift
//  swiftWeibo
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = defaultController()
        
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        setupAppearance()
    
        return true
    }

    func setupAppearance(){
    
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
        
        let tab = UITabBar.appearance()
        tab.tintColor = UIColor.orangeColor()
    
    }

    
    // MARK: - 判断进入哪个控制器
    private func defaultController() -> UIViewController {
        // 用户是否登录, 没有加载到账号表示没有登录
        
        if !JCUserAccount.userLogin {
            // 显示访客视图
            return JCBaseTabBarController()
        }
        // 用户已经登录
        // 是否是新版本,新版本返回新特性界面,否则返回欢迎界面
        return isNewFeature() ? JCNewFeatureController() : JCWelcomeController()
    }

    
    // MARK: - 判断是否是新版本
    /**
    判断是否是新版本
    
    - returns: 是否是新版本
    */
    private func isNewFeature() -> Bool {
        // 获取当前版本
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        
        // 获取上一次的版本(从偏好设置)
        let sandboxVersionKey = "sandboxVersionKey"
        
        // 上一次的版本号
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        // 保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        print("currentVersion: \(currentVersion)")
        print("sandboxVersion: \(sandboxVersion)")
        
        // 比较当前版本和上一次的版本
        return currentVersion != sandboxVersion
    }
    
    
    
    private func switchViewController(isMain: Bool) {
        window?.rootViewController = isMain ? JCBaseTabBarController() : JCWelcomeController()
    }
    class func switchRootViewController(isMain: Bool) {
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(isMain)
    }

    
    
}

